/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import java.util.List;import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.Resource;
import sunsite.po.Usr;
import sunsite.service.FileService;
import sunsite.tools.FileHelper;

/**
 *
 * @author Administrator
 */
public class FileUpload extends ActionSupport {

    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Register.class);
    
    private String type;
    private String rtp;
    private String rcatelog;
    private String rtype;
    private List<sunsite.po.ResourceTp> Resource_tpList;
    private List<sunsite.po.ResourceCatelog> Resource_catelogList;
    private List<sunsite.po.ResourceType> Resource_typeList;
    private List<sunsite.po.Resource> ResourceList;
    
    private String resId;
    private String resName;
    private String address;
    private int status;
    private Date uploadTime;
    private Date lastUpdate;
    private String discription;
    private Usr usr;
    
    private File uploadfile;    
    private String uploadfileFileName;    
    
    private ResourceBundleMessageSource messageSource;
    private FileService fileService;
    
    @Override
    public String execute() throws Exception {
        if (type != null && type.equals("getForm")) {//初始化
            Resource_tpList = fileService.getAllResourceTp();
            Resource_catelogList = new ArrayList();//下拉列表初始化
            Resource_typeList = new ArrayList();//下拉列表初始化
            return INPUT;
        }
        if (type != null && type.equals("rtp")) {//下拉列表可访问类别
            int i = Integer.parseInt(rtp);
            if (i != 0) {
                Resource_catelogList = fileService.getResourceCatelogList(i);
                Resource_typeList = new ArrayList();//清空下拉表
            }
            return INPUT;
        }
        if (type != null && type.equals("rcatelog")) {//下拉列表资源归类
            int i = Integer.parseInt(rcatelog);
            if (i != 0) {
                Resource_typeList = fileService.getResourceTypeList(i);
            }
            return SUCCESS;
        }        
        if (type != null && type.equals("submit")) {//提交
            resId = new Date().getTime()+"";
            String fileName = resId + FileHelper.getExtention(uploadfileFileName);
            File file = new File(ServletActionContext.getServletContext().getRealPath("/UploadFiles") + "/" + fileName);            
            FileHelper.copy(uploadfile, file);//上传
            String filepath="/UploadFiles"+fileName;
            int typeid = Integer.parseInt(rtype);
            if (typeid != 0) {
                Resource resource = new Resource();
                resource.setResId(resId);
                //resource.setUsr(1);
                resource.setResName(resName);
                resource.setResourceType(fileService.getResourceType(typeid));
                resource.setDiscription(discription);
                //resource.setStatus(1);
                resource.setAddress(filepath);
                resource.setUploadTime(new Date(System.currentTimeMillis()));
                fileService.addResource(resource);
            }
            return SUCCESS;
        }
        return SUCCESS;
    }

    public ResourceBundleMessageSource getMessageSource() {
        return messageSource;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public List<sunsite.po.ResourceTp> getResource_tpList() {
        return Resource_tpList;
    }

    public void setResource_tpList(List<sunsite.po.ResourceTp> Resource_tpList) {
        this.Resource_tpList = Resource_tpList;
    }

    public List<sunsite.po.ResourceCatelog> getResource_catelogList() {
        return Resource_catelogList;
    }

    public void setResource_catelogList(List<sunsite.po.ResourceCatelog> Resource_catelogList) {
        this.Resource_catelogList = Resource_catelogList;
    }

    public List<sunsite.po.ResourceType> getResource_typeList() {
        return Resource_typeList;
    }

    public void setResource_typeList(List<sunsite.po.ResourceType> Resource_typeList) {
        this.Resource_typeList = Resource_typeList;
    }

    public String getRtp() {
        return rtp;
    }

    public void setRtp(String rtp) {
        this.rtp = rtp;
    }

    public String getRcatelog() {
        return rcatelog;
    }

    public void setRcatelog(String rcatelog) {
        this.rcatelog = rcatelog;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public FileService getFileService() {
        return fileService;
    }

    public void setFileService(FileService fileService) {
        this.fileService = fileService;
    }

    public String getRtype() {
        return rtype;
    }

    public void setRtype(String rtype) {
        this.rtype = rtype;
    }
    
    /**
     * @return the ResourceList
     */
    public List<sunsite.po.Resource> getResourceList() {
        return ResourceList;
    }

    /**
     * @param ResourceList the ResourceList to set
     */
    public void setResourceList(List<sunsite.po.Resource> ResourceList) {
        this.ResourceList = ResourceList;
    }

    /**
     * @return the resId
     */
    public String getResId() {
        return resId;
    }

    /**
     * @param resId the resId to set
     */
    public void setResId(String resId) {
        this.resId = resId;
    }

    /**
     * @return the resName
     */
    public String getResName() {
        return resName;
    }

    /**
     * @param resName the resName to set
     */
    public void setResName(String resName) {
        this.resName = resName;
    }

    /**
     * @return the address
     */
    public String getAddress() {
        return address;
    }

    /**
     * @param address the address to set
     */
    public void setAddress(String address) {
        this.address = address;
    }

    /**
     * @return the status
     */
    public int getStatus() {
        return status;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(int status) {
        this.status = status;
    }

    /**
     * @return the uploadTime
     */
    public Date getUploadTime() {
        return uploadTime;
    }

    /**
     * @param uploadTime the uploadTime to set
     */
    public void setUploadTime(Date uploadTime) {
        this.uploadTime = uploadTime;
    }

    /**
     * @return the lastUpdate
     */
    public Date getLastUpdate() {
        return lastUpdate;
    }

    /**
     * @param lastUpdate the lastUpdate to set
     */
    public void setLastUpdate(Date lastUpdate) {
        this.lastUpdate = lastUpdate;
    }

    /**
     * @return the discription
     */
    public String getDiscription() {
        return discription;
    }

    /**
     * @param discription the discription to set
     */
    public void setDiscription(String discription) {
        this.discription = discription;
    }

    /**
     * @return the usr
     */
    public Usr getUsr() {
        return usr;
    }

    /**
     * @param usr the usr to set
     */
    public void setUsr(Usr usr) {
        this.usr = usr;
    }
    
    /**
     * @return the uploadfile
     */
    public File getUploadfile() {
        return uploadfile;
    }

    /**
     * @param uploadfile the uploadfile to set
     */
    public void setUploadfile(File uploadfile) {
        this.uploadfile = uploadfile;
    }

    /**
     * @return the uploadfileFileName
     */
    public String getUploadfileFileName() {
        return uploadfileFileName;
    }

    /**
     * @param uploadfileFileName the uploadfileFileName to set
     */
    public void setUploadfileFileName(String uploadfileFileName) {
        this.uploadfileFileName = uploadfileFileName;
    }
    
}
